package com.lixinyu.mp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lixinyu.mp.dto.request.UserPageRequest;
import com.lixinyu.mp.entity.User;
import com.lixinyu.mp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping
    public List<User> findAll(){
        return userService.list();
    }

    @PostMapping
    public Boolean add(@Validated @RequestBody User user){
        return userService.saveOrUpdate(user);
    }

    @PutMapping
    public Boolean update(@Validated @RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    @DeleteMapping("/{id}")
    public Boolean deleteById(@PathVariable Integer id){
        return userService.removeById(id);
    }

    //分页查询语句
    @GetMapping("/{currentPage}/{pageSize}")
    public IPage<User> getPage(@PathVariable int currentPage, @PathVariable int pageSize){
        return userService.getPage(currentPage,pageSize);
    }

    /**
     * POST方式的分页查询+模糊查询
     * @param pageRequest 分页查询请求对象
     * @return 分页结果
     */
    @PostMapping("/{currentPage}/{pageSize}")
    public IPage<User> getUserPageByPost(@PathVariable int currentPage, @PathVariable int pageSize,@RequestBody UserPageRequest pageRequest) {

        // 构建查询条件对象
        User user = new User();
        user.setUserName(pageRequest.getUserName());
        user.setPassword(pageRequest.getPassword());

        // 调用service层分页查询方法
        return userService.getPage(
                currentPage,
                pageSize,
                user
        );


    }

}
